package com.genius.reservation.tools;

import cn.hutool.core.date.DateTime;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public  class  SendWXMsg {

    //parm.put("appid","wxef30e257d686e58f");
    //        parm.put("secret","f926fc51967c95e8d02cbdfab067a90b");
    public  String getToken(){
        String tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxef30e257d686e58f&secret=7a398709277f37e06789e3a552980b72";
        String result = HttpUtil.get(tokenUrl);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        return jsonObject.get("access_token").toString();
    }
    public  void sendSuccessMsg(String openid, String reservationNumber){
        try {
            String msgUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+getToken();
            HashMap<String, Object> parmObj = new HashMap<>();
            parmObj.put("touser",openid);
            parmObj.put("template_id","a3xAfkFkDF9AQqBUQ8-3WYeknvqxkxZDPrekwXA48tA");
            parmObj.put("page","/pages/index/main");
            parmObj.put("miniprogram_state","trial");
            HashMap<String, JSONObject> msgObj = new HashMap<>();
            JSONObject parse1 = new JSONObject().set("value", reservationNumber);
            JSONObject parse2 = new JSONObject().set("value", "收货预约成功");
            JSONObject parse3 = new JSONObject().set("value", DateTime.now().toString("YYYY年MM月dd日 HH:mm"));
            JSONObject parse4 = new JSONObject().set("value", "山西太原阳曲金虎总部");
            JSONObject parse5 = new JSONObject().set("value", "0351-5261111-8021");
            msgObj.put("character_string10",parse1);
            msgObj.put("thing1",parse2);
            msgObj.put("date2",parse3);
            msgObj.put("thing5",parse4);
            msgObj.put("phone_number4",parse5);
            parmObj.put("data",msgObj);
            String s = JSONUtil.toJsonStr(parmObj);
            String post = HttpUtil.post(msgUrl, s);
        }catch (Exception e){
            log.info("{}",e);
            throw  new HttpException("订阅信息发送失败");
        }
    }

    public  void sendCancelMsg(String openid, String cancelReason){
        try {
            String msgUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+getToken();
            HashMap<String, Object> parmObj = new HashMap<>();
            parmObj.put("touser",openid);
            parmObj.put("template_id","NhwOTLgBWAM-elWaPE2oOr-7kKZsrFcYLppZ5r626Mc");
            parmObj.put("page","/pages/index/main");
            parmObj.put("miniprogram_state","trial");
            HashMap<String, JSONObject> msgObj = new HashMap<>();
            JSONObject parse1 = new JSONObject().set("value", "收货预约取消");
            JSONObject parse2 = new JSONObject().set("value", cancelReason);
            JSONObject parse3 = new JSONObject().set("value", "如有疑问请拨打公司电话");
            JSONObject parse4 = new JSONObject().set("value", "0351-5261111-8021");
            JSONObject parse5 = new JSONObject().set("value", DateTime.now().toString("YYYY年MM月dd日 HH:mm"));
            msgObj.put("thing1",parse1);
            msgObj.put("thing6",parse2);
            msgObj.put("thing5",parse3);
            msgObj.put("phone_number4",parse4);
            msgObj.put("date2",parse5);
            parmObj.put("data",msgObj);
            String s = JSONUtil.toJsonStr(parmObj);
            String post = HttpUtil.post(msgUrl, s);
        }catch (Exception e){
            log.info("{}",e);
            throw  new HttpException("订阅信息发送失败");
        }
    }

    public  void sendMsg(String openid, String msg){
        try {
            String msgUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+getToken();
            HashMap<String, Object> parmObj = new HashMap<>();
            parmObj.put("touser",openid);
            parmObj.put("template_id","ttkIMWVTRdt-LBP259mpLqGI4i2R1V73F1giqQA8X6I");
            parmObj.put("page","/pages/index/main");
            parmObj.put("miniprogram_state","trial");
            HashMap<String, JSONObject> msgObj = new HashMap<>();
            JSONObject parse1 = new JSONObject().set("value", "收货预约");
            JSONObject parse2 = new JSONObject().set("value", msg);
            JSONObject parse3 = new JSONObject().set("value", "0351-5261111-8021");
            msgObj.put("thing1",parse1);
            msgObj.put("thing2",parse2);
            msgObj.put("phone_number5",parse3);
            parmObj.put("data",msgObj);
            String s = JSONUtil.toJsonStr(parmObj);
            String post = HttpUtil.post(msgUrl, s);
        }catch (Exception e){
            log.info("{}",e);
            throw  new HttpException("订阅信息发送失败");
        }
    }

}
